
#include "ping.h"

uint16_t in_sksum(uint16_t * addr, int len) {

    int nleft = len;
    uint32_t sum    = 0;
    uint16_t *w = addr;
    uint16_t answer = 0;

    while(nleft > 1) {

        sum += *w++;
        nleft -= 2;
    }
    if (nleft == 1) {

        *(unsigned char *)(&answer) = *(unsigned char *)w;
    }
    // (sum >> 16) 将低32位置零 sum & 0xffff 
    sum = (sum >> 16) + (sum & 0xffff);
    sum += sum >> 16;
    answer = ~sum;

    return answer;
}